package com.bw.gateway.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Objects;

/**
 * @ClassName TokenFile
 * @Author Hao_YunKai
 * @Date 2022/2/22 11:26
 */
//@Component
public class TokenFile implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        String uri = request.getURI().toString();
        System.out.println("请求的路径：" + uri);
        if ("http:localhost:88/api/user/login".equals(uri)) {
            return chain.filter(exchange);
        } else {
            String token = Objects.requireNonNull(request.getHeaders().get("token")).get(0);
            if (StringUtils.isNotBlank(token)) {
                System.out.println("token：" + token);
                return chain.filter(exchange);
            } else {
                return null;
            }
        }
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
